﻿using System.Windows.Controls;
using System.Windows.Data;
using Dsl.Ped.AddIn.Beeps.Generated;
using Dsl.Ped.Framework.Common;

namespace Dsl.Ped.AddIn.Beeps.Projections.LinkedListProjection
{
    /// <summary>
    /// Interaction logic for LinkedListProjection.xaml
    /// </summary>
    public partial class LinkedListProjection : IProjection
    {
        private Beeps.Generated.Beeps parentObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="LinkedListProjection"/> class.
        /// </summary>
        /// <param name="parentObject">The parent object to whose properties we are binding</param>
        /// <param name="membersProperty">The members property to which we are binding</param>
        public LinkedListProjection(Beeps.Generated.Beeps parentObject, string membersProperty)
        {
            InitializeComponent();
            this.DataContext = new LinkedListViewModel();

            this.parentObject = parentObject;

            this.initializeProjection(parentObject, membersProperty);
        }

        #region IProjection

        /// <summary>
        /// Gets the view of the projection
        /// </summary>
        public System.Windows.FrameworkElement View
        {
            get
            {
                return this;
            }
        }

        #endregion

        /// <summary>
        /// Initialize bindings of a projection
        /// </summary>
        /// <param name="parentObject"></param>
        /// <param name="membersProperty"></param>
        private void initializeProjection(object parentObject, string membersProperty)
        {
            // Set the binding of the class name
            Binding membersPropertyBinding = new Binding(membersProperty);
            membersPropertyBinding.Source = parentObject;
            membersPropertyBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            membersPropertyBinding.ValidatesOnDataErrors = true;
            membersPropertyBinding.NotifyOnValidationError = true;
            lstBox.SetBinding(ListBox.ItemsSourceProperty, membersPropertyBinding);   
        }

        /// <summary>
        /// Handler method of the button click event
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Event args</param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            parentObject.Beep.Add(new BeepsBeep() { frequence = 50 });
        }
    }
}
